package com.xncoding.freemarker.utile;

import com.google.common.base.Charsets;
import com.google.common.io.ByteStreams;
import com.google.common.io.CharStreams;
import com.google.common.io.Files;

import java.io.*;

public class GuavaIOExample {

    public static void main(String[] args) {
        File inputFile = new File("/Users/ligang/Desktop/desk/111.txt");
        File outputFile = new File("/Users/ligang/Desktop/desk/output.txt");

        try {
            // 使用ByteStreams和Files工具类从文件中读取字节数据
            byte[] fileContentBytes = Files.toByteArray(inputFile);

            // 将字节数据转换为字符流，这里我们指定UTF-8编码
            // 使用CharStreams来读取和处理字符数据
            String fileContent = new String(fileContentBytes, Charsets.UTF_8);
            System.out.println(fileContent);
            // 对文本内容进行处理将所有字符转换为大写
            String processedContent = fileContent.toUpperCase();

            // 将处理后的字符数据写回文件
            // 首先，我们将字符串转换回字节数据
            byte[] processedBytes = processedContent.getBytes(Charsets.UTF_8);

            // 使用Files和ByteStreams工具类将字节数据写入文件
            try (BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile))) {
//                ByteStreams.write(processedBytes, outputStream);
                ByteStreams.copy(new ByteArrayInputStream(processedBytes), outputStream);
            }

            System.out.println("File processed successfully.");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 注意：优化一版本
    public static void mainWithGuavaStreams(String[] args) throws IOException {
        File inputFile = new File("/Users/ligang/Desktop/desk/111.txt");
        File outputFile = new File("/Users/ligang/Desktop/desk/output.txt");

        // 使用Files工具类创建一个InputStream
        try (FileInputStream fileInputStream = new FileInputStream(inputFile);
             // 使用InputStreamReader将字节流转换为字符流，并指定UTF-8编码
             InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, Charsets.UTF_8);
             // 使用CharStreams.transform处理字符流（这里我们转换为大写）
             InputStreamReader transformedReader = new InputStreamReader(
                     new TransformingInputStream(inputStreamReader, character -> (int) Character.toUpperCase(character)),
                     Charsets.UTF_8);
             // 使用Files工具类创建一个OutputStream
             FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
             // 使用OutputStreamWriter将字符流转换回字节流
             OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, Charsets.UTF_8)) {

            // 使用CharStreams.copy将处理后的字符流复制到输出流
            CharStreams.copy(transformedReader, outputStreamWriter);

            System.out.println("File processed successfully with Guava streams.");
        }
    }

    // CharStreams没提供直接转换字符的功能，自定义一个TransformingInputStream
    private static class TransformingInputStream extends java.io.InputStream {
        private final InputStreamReader reader;
        private final java.util.function.IntFunction<Integer> transformer;

        public TransformingInputStream(InputStreamReader reader, java.util.function.IntFunction<Integer> transformer) {
            this.reader = reader;
            this.transformer = transformer;
        }

        @Override
        public int read() throws IOException {
            int character = reader.read();
            if (character == -1) {
                return -1;
            }
            return transformer.apply(character);
        }

        // 需要重写close方法以确保底层reader也被关闭
        @Override
        public void close() throws IOException {
            reader.close();
        }
    }
}
